package com.iflytek.jcy.supervise.clue.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytek.jcy.supervise.clue.common.enums.EvaluationResultEnum;
import com.iflytek.jcy.supervise.clue.entity.CluePush;
import com.iflytek.jcy.supervise.clue.entity.CluePushDetail;
import com.iflytek.jcy.supervise.clue.mapper.CluePushDetailMapper;
import com.iflytek.jcy.supervise.clue.mapper.CluePushMapper;
import com.iflytek.jcy.supervise.clue.service.ICluePushDetailService;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** <br>
 * 标题:线索推送明细相关的服务类 <br>
 * 描述: <br>
 * 公司: www.iflytek.com<br>
 * 
 * @autho dgyu
 * @time 2022年10月25日 上午11:17:15 */
@Service
public class CluePushDetailServiceImpl extends ServiceImpl<CluePushDetailMapper, CluePushDetail> implements ICluePushDetailService {

	private static final Logger	 logger	= LoggerFactory.getLogger(TaskServiceImpl.class);
	@Autowired
	private CluePushDetailMapper cluePushDetailMapper;
	@Autowired
	private CluePushMapper       cluePushMapper;

	@Override
	public Long getClueNumByRyrm(String rybm) {
		logger.info("getClueNumByRyrm rybm:{}", rybm);
		// 查詢推送的线索数
		LambdaQueryWrapper<CluePushDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(CluePushDetail::getRybm,rybm);
		return this.baseMapper.selectCount(lambdaQueryWrapper);
	}

	@Override
	public Set<String> getChenganBmsah(String rybm) {
		logger.info("getChenganBmsah rybm:{}", rybm);
		Set<String> setAll=new HashSet<>();
		// 查询人员下成案的案号
		LambdaQueryWrapper<CluePushDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(CluePushDetail::getRybm, rybm).eq(CluePushDetail::getEvaluationResult, EvaluationResultEnum.CHENGAN.getCode());
		List<CluePushDetail> list = this.baseMapper.selectList(lambdaQueryWrapper);
		if (CollectionUtils.isNotEmpty(list)) {
			Set<String> set = list.stream().map(CluePushDetail::getBmsah).collect(Collectors.toSet());
			setAll.addAll(set);
		}
		LambdaQueryWrapper<CluePush> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
		lambdaQueryWrapper2.eq(CluePush::getRybm, rybm).eq(CluePush::getEvaluationResult, EvaluationResultEnum.CHENGAN.getCode());
		List<CluePush> list2 = this.cluePushMapper.selectList(lambdaQueryWrapper2);
		if (CollectionUtils.isNotEmpty(list2)) {
			Set<String> set = list2.stream().map(CluePush::getBmsah).collect(Collectors.toSet());
			setAll.addAll(set);
		}
		return setAll;
	}

	@Override
	public int listInquisitorManageFeedback(JSONObject jSONObject) {
		Integer pushDetailId = jSONObject.getInteger("pushDetailId");
		Integer evaluationResult = jSONObject.getInteger("evaluationResult");
		String bmsah = jSONObject.getString("bmsah");
		String ajmc = jSONObject.getString("ajmc");
		String info = jSONObject.getString("info");
		LambdaUpdateWrapper<CluePushDetail> wrapper = new LambdaUpdateWrapper<CluePushDetail>();
		if (evaluationResult != null && evaluationResult >= 0) {
			wrapper.set(CluePushDetail::getEvaluationResult, evaluationResult);
		}
		if (StringUtils.isNotEmpty(bmsah)) {
			wrapper.set(CluePushDetail::getBmsah, bmsah);
		}
		if (StringUtils.isNotEmpty(ajmc)) {
			wrapper.set(CluePushDetail::getAjmc, ajmc);
		}
		if (StringUtils.isNotEmpty(info)) {
			wrapper.set(CluePushDetail::getInfo, info);
		}
		wrapper.set(CluePushDetail::getUpdateTime, new Date());
		wrapper.eq(CluePushDetail::getId, pushDetailId);
		int rows = this.cluePushDetailMapper.update(null, wrapper);
		return rows;
	}
}
